<?php
/*
	* 类与对象 类的自动加载
	* https://www.php.net/manual/zh/language.oop5.autoload.php
	* Adair  zouhdai@gmail.com
	* 2021-08-17
*/

spl_autoload_register(function ($class_name) {
    require_once $class_name . '.php';
});

$obj  = new class19();
$name = $obj->setA('Lucy');
$rst = $obj->getA($name);
var_dump($name,$rst);
//$obj2 = new class21();



spl_autoload_register(function ($name) {
    echo "Want to load $name.\n". "\n<BR \>";
    throw new MissingException("Unable to load $name.");
});

try {
    $obj = new class19();
} catch (Exception $e) {
    echo $e->getMessage(),  "\n<BR \>";
}


class Autoloader
{
    public static function register()
    {

        spl_autoload_register(function ($class) {
        	
            $file = str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
            var_dump($file);
            if (file_exists($file)) {
                require $file;
                return true;
            }
            return false;
        });
    }
}
Autoloader::register();



class autoloader1 {

    public static $loader;

    public static function init()
    {
        if (self::$loader == NULL)
            self::$loader = new self();
        var_dump(self::$loader);
        return self::$loader;
    }

    public function __construct()
    {
    	echo 222;
        spl_autoload_register(array($this,'model'));
        echo 333;
        spl_autoload_register(array($this,'helper'));
        spl_autoload_register(array($this,'controller'));
        spl_autoload_register(array($this,'library'));
    }

    public function library($class)
    {
        set_include_path(get_include_path().PATH_SEPARATOR.'/lib/');
        echo 111;
        spl_autoload_extensions('.php');
        spl_autoload($class);
    }

    public function controller($class)
    {
        $class = preg_replace('/_controller$/ui','',$class);
       
        set_include_path(get_include_path().PATH_SEPARATOR.'/controller/');
        spl_autoload_extensions('.controller.php');
        spl_autoload($class);
    }

    public function model($class)
    {
    	echo 444;
        $class = preg_replace('/_model$/ui','',$class);
       	var_dump($class);
        set_include_path(get_include_path().PATH_SEPARATOR.'/model/');
        spl_autoload_extensions('.model.php');
        spl_autoload($class);
    }

    public function helper($class)
    {
        $class = preg_replace('/_helper$/ui','',$class);

        set_include_path(get_include_path().PATH_SEPARATOR.'/helper/');
        spl_autoload_extensions('.helper.php');
        spl_autoload($class);
    }

}

//call
autoloader1::init();



function ex($parms)
{
   $argvar = func_get_args();
   var_dump($argvar);
   $func = $argvar[0];
   $funcargs = array_slice($argvar,1);
  
if (function_exists($func))
   {
   $returnvalue = call_user_func_array($func,$funcargs);
   }
else
   {
   $funcpath = "scripts/".$func.".php";
   require_once($funcpath);
  
   if (function_exists($func))
      {
      $returnvalue = call_user_func_array($func,$funcargs);
      } 
   else
      {
         die("SORRY  $func IS NOT USABLE");
      }
   }

// return returned value :-)
return $returnvalue;
}

ex($parms = array("1"=>"class19","2"=>"b"));